package com.atguigu.user.service.impl;

import com.atguigu.param.AddressListParam;
import com.atguigu.pojo.Address;
import com.atguigu.user.mapper.AddressMapper;
import com.atguigu.user.service.AddressService;
import com.atguigu.utils.R;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Name: MyComputer
 * @ClassName AddressServiceImpl
 * @Description:
 * @date 2023/5/18 20:10
 */
@Service
public class AddressServiceImpl implements AddressService {

    @Autowired
    private AddressMapper addressMapper;
    /**
     * 根据用户id查询用户 地址数据
     *  1.参数不为空 查询数据库
     *  2.结果封装
     * @param userId
     * @return
     */
    @Override
    public R list(Integer userId) {

//     1.参数不为空 查询数据库
        LambdaQueryWrapper<Address> addressLambdaQueryWrapper = new LambdaQueryWrapper<Address>();
        addressLambdaQueryWrapper.eq(Address::getUserId,userId);
        List<Address> addresses = addressMapper.selectList(addressLambdaQueryWrapper);

//     2.结果封装
        return R.ok("查询成功",addresses);
    }

    /**
     * 插入地址数据插入成功之后返回新的地址集合
     * @param address
     * @return
     */
    @Override
    public R sava(Address address) {

//     1.插入数据
        int insert = addressMapper.insert(address);

//     2.插入成功 返回新的地址集合
        if (insert == 0 ){
            return R.fail("插入地址失败");
        }

        return list(address.getUserId());
    }

    /**
     * 根据id删除地址数据
     * @param id
     * @return
     */
    @Override
    public R remove(Integer id) {

        int i = addressMapper.deleteById(id);

        if (i == 0){
            return R.fail("删除地址失败");
        }

        return R.ok("地址删除成功");
    }
}
